package com.example.horizonserver.controller;


import com.example.horizonserver.common.constant.JwtClaimsConstant;
import com.example.horizonserver.common.properties.JwtProperties;
import com.example.horizonserver.common.result.PageResult;
import com.example.horizonserver.common.result.Result;
import com.example.horizonserver.common.utils.JwtUtil;
import com.example.horizonserver.pojo.DTO.EditPasswordDTO;
import com.example.horizonserver.pojo.DTO.UserDTO;
import com.example.horizonserver.pojo.DTO.UserLoginDTO;
import com.example.horizonserver.pojo.DTO.UserPageQueryDTO;
import com.example.horizonserver.pojo.VO.UserLoginVO;
import com.example.horizonserver.pojo.entity.User;
import com.example.horizonserver.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private UserService userService;

    /**
     * 用户登录
     * @return
     */
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO)
    {
        User user = userService.login(userLoginDTO);
        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, user.getId());
        claims.put(JwtClaimsConstant.USER_ROLE, user.getRole());
        String token = JwtUtil.createJWT(
                jwtProperties.getSecretKey(),
                jwtProperties.getTtl(),
                claims);
        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .username(user.getUsername())
                .name(user.getName())
                .role(user.getRole())
                .token(token)
                .build();
        return Result.success(userLoginVO);
    }

    /**
     * 退出登录
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout()
    {
        return Result.success();
    }

    /**
     * 新增用户
     * @param userDTO
     * @return
     */
    @PostMapping
    public Result<String> save(@RequestBody UserDTO userDTO){
        userService.save(userDTO);
        return Result.success();
    }

    /**
     * 员工批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result<String> delete(@RequestParam List<Integer> ids)
    {
        userService.deleteBatch(ids);
        return Result.success();
    }
    /**
     * 员工条件分页查询
     * @param userPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> page(UserPageQueryDTO userPageQueryDTO)
    {
        PageResult pageResult = userService.pageQuery(userPageQueryDTO);
        return Result.success(pageResult);
    }
    /**
     * 根据id查询员工信息 用作修改回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<User> getById(@PathVariable Integer id)
    {
        User user = userService.getById(id);
        return Result.success(user);
    }
    /**
     * 修改员工信息
     * @param userDTO
     * @return
     */
    @PutMapping
    public Result<String> update(@RequestBody UserDTO userDTO)
    {
        userService.update(userDTO);
        return Result.success();
    }
    /**
     * 修改密码
     * @param editPasswordDTO
     * @return
     */
    @PutMapping("/editPassword")
    public Result<String> editPassword(@RequestBody EditPasswordDTO editPasswordDTO)
    {
        userService.editPassword(editPasswordDTO);
        return Result.success();
    }
}
